package com.snailhd.common.entity;

import java.util.concurrent.*;

/**
 * @author hd
 * @date 2019-12-07 23:07
 */
public class BaseFuture<T> implements Future<T> {


    private CountDownLatch latch = new CountDownLatch(1);

    private T response;

    public BaseFuture(T response) {
        this.response = response;
    }


    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }


    @Override
    public boolean isCancelled() {
        return false;
    }


    @Override
    public boolean isDone() {
        return response != null;
    }


    @Override
    public T get() throws InterruptedException, ExecutionException {
        latch.await();
        return this.response;
    }


    @Override
    public T get(long timeout,TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        if (latch.await(timeout, unit)) {
            return this.response;
        }
        return null;
    }
}
